package com.palmwindActivity.app;


import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;




public abstract class AbstractCommManager {
	public static AbstractCommManager instance;
	
	protected boolean isConnected = false;
	protected Socket connectedSocket = null;
	protected String errorMsg = "";
	
	protected abstract void initSocket(String url, int port)  throws UnknownHostException, IOException;
	
	
	public OutputStream getOutStream()
	{
		if(isConnected && connectedSocket != null)
		{
			try {
				return connectedSocket.getOutputStream();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				errorMsg = e.getMessage();
			}
		}
		return null;
	}
	
	public static AbstractCommManager getInstance()
	{
		
		return  instance;
	}
	
	public InputStream getInputStream()
	{
		if(isConnected && connectedSocket != null)
		{
			try {
				return connectedSocket.getInputStream();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				errorMsg = e.getMessage();
			}
		}
		return null;
	}
	
	public String getErrorMsg()
	{
		return  errorMsg;
	}
	
	public boolean isConnected()
	{
		return isConnected;
	}
	
	public abstract void openConnection() throws UnknownHostException, IOException ;

	public static void init(AbstractCommManager instance2) {
		instance = instance2;
	}


	abstract public void close()throws IOException;
	

}
